package br.pucrio.inf.csm.caching;

import java.util.HashMap;
import java.util.Map;

public class CacheManager {

	private static CacheManager instance;

	private Map<String, Cache> caches = new HashMap<String, Cache>();

	private CacheManager() {
	}

	public static synchronized CacheManager getInstance() {
		if (instance == null) {
			instance = new CacheManager();
		}
		return instance;
	}

	public Cache createCache(String cacheName,
			CachePolicyOption cachePolicyOption, int cacheSize) {
		// LFU is the default cache option
		ICachePolicy cachePolicy = new LFUCachePolicy(cacheSize);
		if (cachePolicyOption == CachePolicyOption.FIFO) {
			cachePolicy = new FIFOCachePolicy();
		} else if (cachePolicyOption == CachePolicyOption.LRU) {
			cachePolicy = new LRUCachePolicy();
		}

		Cache cache = new Cache(cachePolicy);
		caches.put(cacheName, cache);
		
		return cache;
	}
	
	public Cache getCache(String cacheName) {
		return caches.get(cacheName);
	}	
}



